# Contributor: Carbon Jiao <http://archlive-pkg.googlecode.com>

pkgbase=('pidgin')
pkgname=('libpurple' 'pidgin' 'finch')
#pkgver=20100106
pkgver=$(date +%Y%m%d)
pkgrel=1
arch=('i686' 'x86_64')
url="http://pidgin.im/"
license=('GPL')
makedepends=('monotone' 'startup-notification' 'perlxml' 'gtkspell' 'libnice' 'libidn' 'libxss' 'nss' 'libsasl' 'python'
	'hicolor-icon-theme' 'silc-toolkit' 'farsight2' 'tcl' 'tk' 'ca-certificates' 'intltool')
source=(autogen.sh  protocol-makefile.patch config.patch pastie-649601.diff
	Fetion_{16,22,48}.png)

_builddir="${srcdir}/im.pidgin.pidgin"

build() {

	cd "$startdir"
	if [ -f "mtn/pidgin.mtn" ]; then
		msg2 "Updating sources..."
		mtn -d mtn/pidgin.mtn db migrate
		mtn pull -d mtn/pidgin.mtn || return 1
	else
		msg2 "Downloading the mtn source, ..."
		[ -d mtn ] && mkdir mtn
		pushd mtn 1>/dev/null
		wget http://developer.pidgin.im/static/pidgin.mtn.bz2 1>/dev/null 2>/dev/null
		bzip2 -d pidgin.mtn.bz2 1>/dev/null
	fi

	cd "$srcdir"
	msg "Creating build branch..."
	[ -d $srcdir/im.pidgin.pidgin ] && rm -rf "$srcdir/im.pidgin.pidgin"
	mtn -d $startdir/mtn/pidgin.mtn co -b im.pidgin.pidgin

	msg "Fetch protocl of Chinese Fetion"
	git clone git://github.com/gradetwo/fetion.git $srcdir/fetion
	rm -rf `find $srcdir/fetion -name .git`
	cp -r $srcdir/fetion ${_builddir}/libpurple/protocols/
	cp $srcdir/autogen.sh ${_builddir}/

	cd ${_builddir}
	patch -Np1 -i $srcdir/config.patch || return 1
	patch -Np1 -i $srcdir/protocol-makefile.patch || return 1

	#patch pidgin for msn plus plugin
	patch -Np1 -i $srcdir/pastie-649601.diff || return 1


	msg "Start to compile and install pidgin"
	./autogen.sh --prefix=/usr --sysconfdir=/etc --disable-schemas-install --disable-meanwhile \
		--disable-nm --disable-perl --disable-gnutls --enable-cyrus-sasl --enable-vv --disable-doxygen \
		--with-system-ssl-certs=/etc/ssl/certs || return 1

	make || return 1
}

package_libpurple(){
	pkgdesc="IM library extracted from Pidgin, with Chinese Fetion support"
	depends=('farsight2' 'libsasl' 'dbus-glib' 'silc-toolkit' 'nss')

	cd ${_builddir}

	for dir in libpurple share/sounds share/ca-certs m4macros po; do
		make -C "$dir" DESTDIR="$pkgdir" install || return 1
	done

#	# Remove GConf schema file
#	rm -rf "$pkgdir/etc" || return 1

#	# Remove libpurple and pidgin development headers
#	rm -rf "$pkgdir/usr/include" || return 1

#	# Remove libpurple aclocal macro file
#	rm -rf "$pkgdir/usr/share/aclocal" || return 1

#	# 删除中文外的语言包  Delete the locale files except Chinese
#	find ${pkgdir}/usr/share/locale/* -maxdepth 0 -type d ! -iregex ".*zh.CN" -exec rm -rf {} \;
}

package_pidgin(){
	pkgdesc="Multi-protocol instant messaging client from latest mtn database"
	depends=("libpurple=$pkgver" 'startup-notification' 'gtkspell' 'libxss' 'python'
		'hicolor-icon-theme' 'gstreamer0.10-base')
	optdepends=('gstreamer0.10-good: to play sounds'
                'avahi: Bonjour protocol support'
                'tk: Tcl/Tk scripting support'
                'ca-certificates: SSL CA certificates'
                'aspell: for spelling correction')
	install=pidgin.install

	cd ${_builddir}
	#for linking
	make -C libpurple DESTDIR="$pkgdir" install-libLTLIBRARIES || return 1

	make -C pidgin DESTDIR="$pkgdir" install || return 1
	make -C doc DESTDIR="$pkgdir" install || return 1

	#clean up libpurple
	make -C libpurple DESTDIR="$pkgdir" uninstall-libLTLIBRARIES || return 1

	install -D -m 0644 pidgin.desktop "$pkgdir"/usr/share/applications/pidgin.desktop || return 1

	# Install Fetion protols icons
	for i in 16 22 48; do
		install -m644 $startdir/Fetion_$i.png $pkgdir/usr/share/pixmaps/pidgin/protocols/$i/fetion.png
	done
	#install -m644 $startdir/Fetion.svg $pkgdir/usr/share/pixmaps/pidgin/protocols/scalable/fetion.svg

	rm -f "$pkgdir"/usr/share/man/man1/finch.1 || return 1
}
package_finch(){
	pkgdesc="A ncurses-based messaging client"
	depends=("libpurple=$pkgver" 'libidn')
	optdepends=('avahi: Bonjour protocol support'
    		'tk: Tcl/Tk scripting support'
		'ca-certificates: SSL CA certificates')

	cd ${_builddir}
	#for linking
	make -C libpurple DESTDIR="$pkgdir" install-libLTLIBRARIES || return 1

	make -C finch DESTDIR="$pkgdir" install || return 1
	make -C doc DESTDIR="$pkgdir" install || return 1

	#clean up libpurple
	make -C libpurple DESTDIR="$pkgdir" uninstall-libLTLIBRARIES || return 1
	rm -f "$pkgdir"/usr/share/man/man1/pidgin.1 || return 1
}
md5sums=('007c6b13756fa32e03ea39a8478fec1a'
         'ff36559a625f27e33c0760333b4c383f'
         'a6fa1cecf8c4d600d2e6db51fd4786c9'
         'eb60e05ef5945b9be784230e466ef9a2'
         'f5b0ad9e3a055bdda1a06793e067f894'
         '46c3ac92a74eb8f044c668e1af294b57'
         'c64c1030be6dd4cf0a2328e324623a62')
